package com.ge.modules.api.service.impl;

import com.ge.modules.api.service.TbOrderService;
import com.ge.modules.api.service.TbStoreService;
import com.ge.modules.api.service.TbTechnicianService;
import com.ge.modules.dao.*;
import com.ge.modules.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TbOrderServiceImpl implements TbOrderService {
    @Autowired
    private TbOrderDao tbOrderDao;

    @Autowired
    private TbOrderHeaderDao orderHeaderDao;

    @Autowired
    private TbCommissionIncomeinfoDao tbCommissionIncomeinfoDao;

    @Autowired
    private TbUserDao userDao;

    @Autowired
    private TbUserCardDao userCardDao;

    @Autowired
    private TbStoreService tbStoreService;

    @Autowired
    private TbTechnicianService tbTechnicianService;

    @Override
    public TbOrderEntity queryObject(TbOrderEntity tbOrderEntity) {
        return tbOrderDao.queryObject(tbOrderEntity);
    }

    @Override
    public List<TbOrderEntity> queryList(Map<String, Object> map) {
        return tbOrderDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return tbOrderDao.queryTotal(map);
    }

    @Override
    public void save(TbOrderEntity order) {
        tbOrderDao.save(order);
    }

    @Override
    public void update(TbOrderEntity order) {
        tbOrderDao.update(order);
        //如果订单状态为已完成 则添加收入明细
        if (order.getOrderStatus() != null && order.getOrderStatus() == 5) {
            //获取店铺收入明细
            TbStoreEntity tbStoreEntity = tbStoreService.queryByStoreId(order.getCommodityMasterStoreId());
            TbCommissionIncomeinfoEntity tbCommissionIncomeinfoEntity = new TbCommissionIncomeinfoEntity();
            tbCommissionIncomeinfoEntity.setCommodityId(order.getCommodityId());
            tbCommissionIncomeinfoEntity.setCommodityName(order.getCommodityName());
            tbCommissionIncomeinfoEntity.setCreateTime(new Date());
            tbCommissionIncomeinfoEntity.setIncomeinfoDescribe("客户：" + order.getContact() + "消费了 " + order.getOrderPrice() + " 元");
            tbCommissionIncomeinfoEntity.setIncomeinfoMoney(order.getOrderPrice());
            //收入类型 为商品
            tbCommissionIncomeinfoEntity.setIncomeinfoType(1);
            tbCommissionIncomeinfoEntity.setStoreId(order.getCommodityMasterStoreId());
            tbCommissionIncomeinfoEntity.setStoreName(order.getStoreName());
            tbCommissionIncomeinfoEntity.setUserId(tbStoreEntity.getStoreId());
            tbCommissionIncomeinfoEntity.setUserType(tbStoreEntity.getUserType());
            tbCommissionIncomeinfoEntity.setOrderId(order.getId());
            tbCommissionIncomeinfoDao.save(tbCommissionIncomeinfoEntity);

            //添加技师提成收入明细
            TbTechnicianEntity tbTechnicianEntity = tbTechnicianService.queryByTechnicianId(order.getCommodityMasterId());
            String rate = tbTechnicianEntity.getCommissionRate();
            //从用户角度来输入的百分比 进行除以100 方便计算
            Double dRate = Double.valueOf(rate) / 100;
            TbCommissionIncomeinfoEntity tbCommissionIncomeinfoEntity2 = new TbCommissionIncomeinfoEntity();
            tbCommissionIncomeinfoEntity2.setCommodityId(order.getCommodityId());
            tbCommissionIncomeinfoEntity2.setCommodityName(order.getCommodityName());
            tbCommissionIncomeinfoEntity2.setCreateTime(new Date());
            tbCommissionIncomeinfoEntity2.setIncomeinfoDescribe("客户：" + order.getContact() + "消费了 " + order.getOrderPrice() + " 元" +"获得提成："+order.getOrderPrice().multiply(BigDecimal.valueOf(dRate)));
            //提成 = 订单总价 * 技师的提成比例
            tbCommissionIncomeinfoEntity2.setIncomeinfoMoney(order.getOrderPrice().multiply(BigDecimal.valueOf(dRate)));
            //收入类型 为提成
            tbCommissionIncomeinfoEntity2.setIncomeinfoType(3);
            tbCommissionIncomeinfoEntity2.setStoreId(order.getCommodityMasterStoreId());
            tbCommissionIncomeinfoEntity2.setStoreName(order.getStoreName());
            tbCommissionIncomeinfoEntity2.setUserId(tbTechnicianEntity.getTechnicianId());
            tbCommissionIncomeinfoEntity2.setUserType(tbTechnicianEntity.getUserType());
            tbCommissionIncomeinfoEntity2.setOrderId(order.getId());
            tbCommissionIncomeinfoDao.save(tbCommissionIncomeinfoEntity2);
        }
    }

    @Override
    public void delete(Integer id) {
        tbOrderDao.delete(id);
    }

    @Override
    public void deleteBatch(Integer[] ids) {
        tbOrderDao.deleteBatch(ids);
    }

    @Override
    public void updateBatch(Integer[] ids) {
        tbOrderDao.updateBatch(ids);
    }

    @Override
    public void orderAndOrderHeader(List<TbOrderEntity> tbOrderEntityList, TbOrderHeaderEntity tbOrderHeaderEntity) {
        for (int i = 0; i < tbOrderEntityList.size(); i++) {
            tbOrderDao.save(tbOrderEntityList.get(i));
        }
        orderHeaderDao.save(tbOrderHeaderEntity);
    }

    @Override
    public Map queryOrdersAndTotal(Map<String, Object> map) {
        return tbOrderDao.queryOrdersAndTotal(map);
    }

    @Override
    public void updateOrderAndUserCard(TbOrderEntity order, TbUserCardEntity userCard) {
        tbOrderDao.update(order);
        userCardDao.update(userCard);
        //修改用户积分
//        TbUser tbUser = userDao.selectByPrimaryKey(order.getUserId());
//        tbUser.setIntegral(tbUser.getIntegral() + order.getOrderPrice());
//        userDao.update(tbUser);

    }

    @Override
    public void updateOrder(TbOrderEntity order) {
        tbOrderDao.update(order);
        //修改用户积分
//        TbUser tbUser = userDao.selectByPrimaryKey(order.getUserId());
//        tbUser.setIntegral(tbUser.getIntegral() + order.getOrderPrice());
//        userDao.update(tbUser);

    }

    @Override
    public int countStoreUsers(Map<String, Object> map) {
        return tbOrderDao.countStoreUsers(map);
    }

}
